using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using KMS.Core.application;
using KMS.Web.Views.Projects;
using KMS.Core.domain.model.post;
using KMS.Web.Views.Shared;

namespace KMS.Web.Controllers
{
    public class ProjectsController : Controller
    {
        private IProjectsService _projectService;

        public ActionResult UserProjects()
        {
            return View(_projectService.FindProjectsForCurrentUser().Select(p => new ProjectViewModel(p, new List<Resource>())).ToList());
        }

        public ProjectsController(IProjectsService projectsService)
        {
            _projectService = projectsService;
        }

        public ViewResult GoTo(int projectId)
        {
            return View(new ProjectViewModel(_projectService.FindProject(projectId), _projectService.FindResourcesForProject(projectId)));
        }

        public ViewResult AddResource(int projectId)
        {
            var project = _projectService.FindProject(projectId);
            return View(new ResourceViewModel { Project = new ProjectViewModel { Name = project.Name } });
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ViewResult AddResource([Bind(Exclude = "Id")] ResourceViewModel resourceViewModel, int projectId)
        {
            _projectService.AddResourceToProject(resourceViewModel.Title, resourceViewModel.Content, projectId);
            return View();
        }  
    }
}
